﻿Public Class wucProductMaint
    Inherits System.Web.UI.UserControl

    Public Property IdProduct As String
        Get
            Return Me.txtID.Text
        End Get
        Set(ByVal value As String)
            Me.txtID.Text = value
            Me.showReg()
        End Set
    End Property

    Public Property HeaterText As String
        Get
            Return Me.header.Text
        End Get
        Set(ByVal value As String)
            Me.header.Text = value
        End Set
    End Property

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
        If Val(Me.txtID.Text) > 0 Then
            Me.UpdateRegister()
        Else
            Me.NewRegister()
        End If
    End Sub

    Private Sub showReg()
        Dim conStr As String = "Data Source=72.18.135.49,1533;Initial Catalog=DBGio;Persist Security Info=True;User ID=gio;Password=admin123"

        Dim sqlcon As New SqlClient.SqlConnection(conStr)

        Try
            sqlcon.Open()

            Dim strSQL As String = " select * from Products where idProducts=" & Me.IdProduct

            Dim dbcomm As New Data.SqlClient.SqlCommand(strSQL, sqlcon)

            Dim da As New Data.SqlClient.SqlDataAdapter
            da.SelectCommand = dbcomm

            'Dim dbreader = dbcomm.ExecuteReader()

            Dim dt As New DataTable
            da.Fill(dt)
            'dt.Load(dbreader)

            Me.txtName.Text = dt.Rows(0).Item(1).ToString
            Me.txtUnit.Text = dt.Rows(0).Item(2).ToString
            Me.txtSupplier.Text = dt.Rows(0).Item(3).ToString
            Me.btnSubmit.Text = "Update"

            da.Dispose()
            sqlcon.Close()
        Catch ex As Exception
            Response.Write(ex.Message.ToString)
        Finally
            sqlcon.Close()
        End Try
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
        Me.txtName.Text = ""
        Me.txtUnit.Text = ""
        Me.txtSupplier.Text = ""
    End Sub

    Private Sub InsertUpdateDelete()
        'more coding here
    End Sub

    Public Sub NewRegister()
        'Dim conStr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbTemp.mdf;Integrated Security=True;User Instance=True"
        Dim conStr As String = "Data Source=72.18.135.49,1533;Initial Catalog=DBGio;Persist Security Info=True;User ID=gio;Password=admin123"

        Dim sqlcon As New SqlClient.SqlConnection(conStr)
        Try
            sqlcon.Open()

            Dim strSQL As String = "insert into Products(name, units, supplier) values('" & Me.txtName.Text & "', " & Me.txtUnit.Text & ", '" & Me.txtSupplier.Text & "')" '" select * from Products "
            Dim dbcomm As New Data.SqlClient.SqlCommand(strSQL, sqlcon)

            Dim da As New Data.SqlClient.SqlDataAdapter
            da.SelectCommand = dbcomm

            dbcomm.ExecuteNonQuery()
            da.Dispose()
            sqlcon.Close()
            Response.Redirect(Me.Request.Path)
        Catch ex As Exception
            Response.Write(ex.Message.ToString)
        Finally
            sqlcon.Close()
        End Try
    End Sub

    Public Sub UpdateRegister()
        'Dim conStr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbTemp.mdf;Integrated Security=True;User Instance=True"
        Dim conStr As String = "Data Source=72.18.135.49,1533;Initial Catalog=DBGio;Persist Security Info=True;User ID=gio;Password=admin123"

        Dim sqlcon As New SqlClient.SqlConnection(conStr)
        Try
            sqlcon.Open()

            Dim strSQL As String = "update Products set name='" & Me.txtName.Text & "', units='" & Val(Me.txtUnit.Text) & "', supplier='" & Me.txtSupplier.Text & "' where idProducts=" & Me.txtID.Text
            Dim dbcomm As New Data.SqlClient.SqlCommand(strSQL, sqlcon)

            Dim da As New Data.SqlClient.SqlDataAdapter
            da.SelectCommand = dbcomm

            dbcomm.ExecuteNonQuery()
            da.Dispose()
            sqlcon.Close()
            Response.Redirect(Me.Request.Path)
        Catch ex As Exception
            Response.Write(ex.Message.ToString)
        Finally
            sqlcon.Close()
        End Try
    End Sub
End Class